package avicit.bdp.dms.das.utils;

import avicit.bdp.common.utils.DateUtils;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
import java.security.MessageDigest;
import java.security.spec.AlgorithmParameterSpec;

public class SecurityUtil {

    private static final byte[] RGBKEY = {78, 47, 79, 28, 66, 97, 32, 63};
    private static final byte[] RGBIV = {24, 27, 31, 53, 66, 83, 33, 42};

    private SecurityUtil() throws Exception {
    }

    /**
     * DES加密字符串
     *
     * @param encrStr 要加密的字符串
     * @return
     * @throws Exception
     */
    public static String encrypt(String encrStr) {
        return SecurityUtil.encryptURLSafe(encrStr);
    }

    /**
     * 加密为URL安全的字符串
     *
     * @param encrStr
     * @return
     * @throws Exception
     */
    public static String encryptURLSafe(String encrStr) {
        try {
            DESKeySpec keySpec = new DESKeySpec(RGBKEY);
            AlgorithmParameterSpec iv = new IvParameterSpec(RGBIV);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            Key key = keyFactory.generateSecret(keySpec);
            Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            enCipher.init(Cipher.ENCRYPT_MODE, key, iv);
            byte[] pasByte = enCipher.doFinal(encrStr.getBytes("utf-8"));
            String result = Base64.encodeBase64URLSafeString(pasByte);
            return filter(result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static String filter(String str) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            int asc = str.charAt(i);
            if (asc != 10 && asc != 13) {
                sb.append(str.subSequence(i, i + 1));
            }
        }
        return new String(sb);
    }

    public static void main(String[] args) {
        //System.out.println(SecurityUtil.decrypt("snTGuVWqHac"));
        //System.out.println(SecurityUtil.encrypt("123456"));
        String a = DateUtils.getFirstDayOfMonth("2020-07");
        System.out.println(a);

    }

    /**
     * DES解密字符串
     *
     * @param decrStr 要解密的字符串
     * @return
     * @throws Exception
     */
    public static String decrypt(String decrStr) {
        try {
            DESKeySpec keySpec = new DESKeySpec(RGBKEY);
            AlgorithmParameterSpec iv = new IvParameterSpec(RGBIV);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            Key key = keyFactory.generateSecret(keySpec);
            Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            deCipher.init(Cipher.DECRYPT_MODE, key, iv);
            byte[] pasByte = deCipher.doFinal(Base64.decodeBase64(decrStr));
            String s = new String(pasByte, "UTF-8");
            return s;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String byteArrayMD5(byte[] data) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(data, 0, data.length);
            return new String(Hex.encodeHex(md5.digest()));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
